home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / gtk_treeviewtooltips.py < prev    next >
Encoding:
Python Source  |  2009-05-05  |  7.9 KB  |  203 lines

  1. #
  2. # Copyright 2007 Red Hat, Inc.
  3. # Authors:
  4. # Thomas Woerner <twoerner@redhat.com>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19.  
  20. import pygtk
  21. import gtk
  22. import gobject
  23.  
  24. ##############################################################################
  25.  
  26. class TreeViewTooltips(gobject.GObject):
  27.     __gproperties__ = {
  28.         "show-delay": (gobject.TYPE_UINT,
  29.                        "Show Delay (ms)", "Show Delay (ms), 0 for no delay.",
  30.                        0, 10000, 500,
  31.                        gobject.PARAM_READWRITE),
  32.         "hide-delay": (gobject.TYPE_UINT,
  33.                        "Hide Delay (ms)", "Hide Delay (ms), 0 for unlimited.",
  34.                        0, 99999999, 5*1000,
  35.                        gobject.PARAM_READWRITE),
  36.         }
  37.  
  38.     def __init__(self, treeview, tooltip_func, *tooltip_func_args):
  39.         self.__gobject_init__()
  40.         self.treeview = treeview
  41.         self.tooltip_func = tooltip_func
  42.         self.tooltip_func_args = tooltip_func_args
  43.  
  44.         self.popup = gtk.Window(gtk.WINDOW_POPUP)
  45.         self.label = gtk.Label()
  46.         self.label.set_line_wrap(True)
  47.         self.label.set_use_markup(True)
  48.         self.popup.add(self.label)
  49.         self.popup.set_name('gtk-tooltips')
  50.         self.popup.set_resizable(False)
  51.         self.popup.set_border_width(4)
  52.         self.popup.set_app_paintable(True)
  53.  
  54.         self.show_delay = 500
  55.         self.hide_delay = 5*1000
  56.  
  57.         self.show_timer = None
  58.         self.hide_timer = None
  59.         self.path = self.col = None
  60.  
  61.         treeview.connect("motion-notify-event", self.on_motion_notify)
  62.         treeview.connect("leave-notify-event", self.on_leave_notify)
  63.         self.popup.connect("expose-event", self.on_expose_event)
  64.  
  65.     def do_get_property(self, property):
  66.         if property.name == 'show-delay':
  67.             return self.show_delay
  68.         elif property.name == 'hide-delay':
  69.             return self.hide_delay
  70.  
  71.     def do_set_property(self, property, value):
  72.         if property.name == 'show-delay':
  73.             self.show_delay = value
  74.         elif property.name == 'hide-delay':
  75.             self.hide_delay = value
  76.  
  77.     def on_motion_notify(self, treeview, event):
  78.         if event.window != treeview.get_bin_window():
  79.             return
  80.  
  81.         path = treeview.get_path_at_pos(int(event.x), int(event.y))
  82.         if path:
  83.             path, col, _x, _y = path
  84.             if self.path != path or self.col != col:
  85.                 if self.hide_timer:
  86.                     gobject.source_remove(self.hide_timer)
  87.                     self.hide_timer = None
  88.                 self.hide_tip()
  89.                 self.path = path
  90.                 self.col = col
  91.                 if self.show_timer:
  92.                     gobject.source_remove(self.show_timer)
  93.                     self.show_timer = None
  94.                 self.show_timer = gobject.timeout_add(self.show_delay,
  95.                                                       self.show_tip,
  96.                                                       path, col,
  97.                                                       event.x, event.y)
  98.             else:
  99.                 if self.hide_timer:
  100.                     gobject.source_remove(self.hide_timer)
  101.                     if self.hide_delay > 0:
  102.                         self.hide_timer = gobject.timeout_add(self.hide_delay, 
  103.                                                               self.hide_tip)
  104.         elif self.path or self.col:
  105.             if self.show_timer:
  106.                 gobject.source_remove(self.show_timer)
  107.                 self.show_timer = None
  108.             if self.hide_timer:
  109.                 gobject.source_remove(self.hide_timer)
  110.                 self.hide_timer = None
  111.             self.hide_tip()
  112.  
  113.     def on_leave_notify(self, treeview, event):
  114.         if self.show_timer:
  115.             gobject.source_remove(self.show_timer)
  116.             self.show_timer = None
  117.         if self.hide_timer:
  118.             gobject.source_remove(self.hide_timer)
  119.             self.hide_timer = None
  120.         if self.path or self.col:
  121.             self.hide_tip()
  122.  
  123.     def show_tip(self, path, col, event_x, event_y):
  124.         text = self.tooltip_func(self.treeview.get_model(), path, col,
  125.                                  *self.tooltip_func_args)
  126.         if not text or len(text) == 0:
  127.             return False
  128.         self.label.set_label(text)
  129.         (parent_x, parent_y) = self.treeview.get_bin_window().get_origin()
  130.         (width, height) = self.popup.size_request()
  131.         screen_width = gtk.gdk.screen_width()
  132.         screen_height = gtk.gdk.screen_height()
  133.         x = int(event_x + parent_x)
  134.         y = int(event_y + parent_y + 10)
  135.         if x + width> screen_width:
  136.             x = screen_width - width
  137.         if y + height > screen_height:
  138.             y = int(event_y + parent_y - height - 10)
  139.         self.popup.move(x, y)
  140.         self.popup.show_all()
  141.         if self.hide_delay > 0:
  142.             self.hide_timer = gobject.timeout_add(self.hide_delay,
  143.                                                   self.hide_tip)
  144.         return False
  145.  
  146.     def on_expose_event(self, window, event):
  147.         (width, height) = window.size_request()
  148.         window.style.paint_flat_box(window.window, gtk.STATE_NORMAL,
  149.                                     gtk.SHADOW_OUT, None, window,
  150.                                     'tooltip', 0, 0, width, height)
  151.  
  152.     def hide_tip(self):
  153.         self.path = self.col = None
  154.         self.popup.hide()
  155.         return False
  156.  
  157. ##############################################################################
  158.  
  159. if __name__ == "__main__":
  160.     def getTooltip(model, path, col, cell):
  161.         iter = model.get_iter(path)
  162.         text = "%s" % model.get_value(iter, cell)
  163.         return text
  164.  
  165.     window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  166.     window.connect("delete_event", gtk.main_quit)
  167.     window.set_default_size(200,250)
  168.         
  169.     scrolledwin = gtk.ScrolledWindow()
  170.     scrolledwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  171.     window.add(scrolledwin)
  172.         
  173.     treeview = gtk.TreeView()
  174.     treeview.get_selection().set_mode(gtk.SELECTION_NONE)
  175.     store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING,
  176.                           gobject.TYPE_STRING)
  177.     treeview.set_model(store)
  178.     scrolledwin.add(treeview)
  179.  
  180.     column = gtk.TreeViewColumn("Head1", gtk.CellRendererText(), text=0)
  181.     treeview.append_column(column)
  182.  
  183.     column = gtk.TreeViewColumn("Head2", gtk.CellRendererText(), text=1)
  184.     treeview.append_column(column)
  185.  
  186.     for i in xrange(10):
  187.         cell1 = "cell data %d.1" % i
  188.         cell2 = "cell data %d.2" % i
  189.         cell3 = "Tooltip %d" % i
  190.         cell3 += """
  191. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  192.  
  193. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  194.  
  195. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA."""
  196.         store.append([cell1, cell2, cell3])
  197.     
  198.     tips = TreeViewTooltips(treeview, getTooltip, 2)
  199. #    tips.set_property("hide-delay", 20*1000)
  200.  
  201.     window.show_all()
  202.     gtk.main()
  203.